/**
 * Eclipse Outliner
 * Copyright (C) 2006 Jeff Mesnil
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.jmesnil.opml.core.internal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jmesnil.opml.core.IBody;
import net.jmesnil.opml.core.IOutline;
import net.jmesnil.opml.core.IOutlineNode;

public class Body implements IBody {

    private List outlines = new ArrayList();

    public Body() {
    }

    public IOutline[] getChildren() {
        return (IOutline[]) outlines.toArray(new IOutline[outlines.size()]);
    }

    public IOutline removeChild(IOutline outlineToRemove) {
        Outline outline = (Outline) outlineToRemove;
        boolean found = outlines.remove(outline);
        if (!found) {
            return null;
        }
        outline.setParent(null);
        return outline;
    }

    public IOutline appendChild(IOutline outline) {
        outlines.add(outline);
        return outline;
    }

    public IOutline appendNewOutline(String text) {
        IOutline outline = new Outline(this, text, new Date());
        outlines.add(outline);
        return outline;
    }

    public IOutline insertAfter(IOutline outline, IOutlineNode refOutline) {
        if (outline == null) {
            return null;
        }
        int index = outlines.indexOf(refOutline);
        if (index == -1) {
            return null;
        } else {
            Outline outlineImpl = (Outline) outline;
            IOutlineNode oldParent = outline.getParent();
            outlineImpl.parent = this;
            outlines.add(index + 1, outline);
            if (oldParent != null) {
                oldParent.removeChild(outline);
            }
        }
        return outline;
    }

    public IOutline insertAfter(String text, IOutline refOutline) {
        int index = outlines.indexOf(refOutline);
        Outline newOutline = new Outline(this, text, new Date());
        if (index == -1) {
            outlines.add(newOutline);
        } else {
            outlines.add(index + 1, newOutline);
        }
        return newOutline;
    }

    public String toXML() {
        StringBuffer xml = new StringBuffer("<body>\n");
        for (int i = 0; i < outlines.size(); i++) {
            IOutline outline = (IOutline) outlines.get(i);
            xml.append(outline.toXML());
        }
        xml.append("</body>\n");
        return xml.toString();
    }

    public IOutlineNode getParent() {
        return null;
    }

    public IOutline getPreviousSibling() {
        return null;
    }

    public IOutline getNextSibling() {
        return null;
    }

}
